//
//  TimeView.m
//  Park
//
//  Created by 123456 on 2017/11/28.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "TimeView.h"


@implementation TimeView
{
    NSString *year;
    NSString *month;
    NSString *day;
    NSString *monthstr;//判断月份
    NSString *daystr;//判断天数
    BOOL isdate;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.CancelBUtton.layer.cornerRadius = 5;
    self.CancelBUtton.layer.cornerRadius = 5.0f;
    self.CancelBUtton.layer.borderWidth =0.8f;
    self.CancelBUtton.layer.borderColor = [UIColor grayColor].CGColor;
    self.CancelBUtton.layer.masksToBounds = YES;
    
    self.ConfirmBUtton.layer.cornerRadius = 5;
    self.ConfirmBUtton.layer.cornerRadius = 5.0f;
    self.ConfirmBUtton.layer.borderWidth =0.8f;
    self.ConfirmBUtton.layer.borderColor = [UIColor grayColor].CGColor;
    self.ConfirmBUtton.layer.masksToBounds = YES;

    self.Datebutton.layer.cornerRadius = 15;
    self.Datebutton.layer.cornerRadius = 15.0f;
    self.Datebutton.layer.borderWidth =0.8f;
    self.Datebutton.layer.borderColor = [UIColor grayColor].CGColor;
    self.Datebutton.layer.masksToBounds = YES;

    
    self.TimerPicker.delegate = self;
    self.TimerPicker.dataSource = self;
    self.TimerPicker.backgroundColor = RGB(26, 32, 40, 1);
    self.backgroundColor = RGB(26, 32, 40, 1);
    self.TimerPicker.alpha = 0.9;
    self.alpha = 0.9;

    
    isdate = YES;
    
    [self update];

  

}

//第一次数据加载
-(void)update{
    NSDate * date = [NSDate date];//当前时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    
    formatter.dateFormat = @"YYYY";
    formatter1.dateFormat = @"MM";
    formatter2.dateFormat = @"dd";
    [self.TimerArray removeAllObjects];
    [self.TimerArray1 removeAllObjects];
    [self.TimerArray2 removeAllObjects];

    year = [formatter stringFromDate:date];
    for (int i=2015; i<=year.intValue; i++) {
        [self.TimerArray  addObject:[NSString stringWithFormat:@"%d",i]];
    }
    month = [formatter1 stringFromDate:date];
    monthstr =[formatter1 stringFromDate:date];
    for (int i = 1; i<=month.intValue; i++) {
        
        if (i<=9) {
            [self.TimerArray1 addObject:[NSString stringWithFormat:@"0%d",i]];

        }else{
        [self.TimerArray1 addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    day = [formatter2 stringFromDate:date];
    daystr =[formatter2 stringFromDate:date];
    
    for (int i=1; i<=day.integerValue; i++) {
        
        if (i<=9) {
            [self.TimerArray2 addObject:[NSString stringWithFormat:@"0%d",i]];
        }else{
            [self.TimerArray2 addObject:[NSString stringWithFormat:@"%d",i]];
        }

    }
    
    self.DateLabel.text = [NSString stringWithFormat:@"%@年-%@月",year,month];
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    
    if (isdate == YES) {
        return 2;
    }else{
        return 3;
    }
}

// 返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    
    if (isdate == YES) {
        if (component == 1) {
            return self.TimerArray1.count;
        }
        return self.TimerArray.count;
    }else{
    
    
    if (component == 1){
        return self.TimerArray1.count;
    }
    if (component ==2) {
        
        return self.TimerArray2.count;
    }
    return self.TimerArray.count;
    }
    
}



// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (isdate ==YES) {
        if (component ==1) {
            return self.TimerArray1[row];
        }
        return self.TimerArray[row];
        

    }else{
        
    if (component == 1){
        return self.TimerArray1[row];
    }
    if (component ==2) {
        return self.TimerArray2[row];
    }
    return self.TimerArray[row];

    }
    
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    

    if (isdate ==YES) {

            if (component ==0) {
                year = self.TimerArray[row];

            }else if (component ==1){
                month = self.TimerArray1[row];
                
                NSLog(@"%d == %d",monthstr.intValue,month.intValue);
                if (monthstr.intValue == month.intValue) {
                    [self.TimerArray2 removeAllObjects];
                    
                    for (int i=1; i<=daystr.integerValue; i++) {
                        
                        if (i<=9) {
                            [self.TimerArray2 addObject:[NSString stringWithFormat:@"0%d",i]];
                            
                        }else{
                            [self.TimerArray2 addObject:[NSString stringWithFormat:@"%d",i]];
                        }
                    }
                    
                }else{
                    [self.TimerArray2 removeAllObjects];
                    
                    for (int i=1; i<=[self howManyDaysInThisYear:year.integerValue withMonth:month.integerValue]; i++) {
                        if (i<=9) {
                            [self.TimerArray2 addObject:[NSString stringWithFormat:@"0%d",i]];
                            
                        }else{
                            [self.TimerArray2 addObject:[NSString stringWithFormat:@"%d",i]];
                        }
                    }
                    
                }

            }
        self.DateLabel.text = [NSString stringWithFormat:@"%@年-%@月",year,month];



    }else{

        if (component ==0) {
            year = self.TimerArray[row];
            
        }else if (component ==1){

            month = self.TimerArray1[row];
            
            NSLog(@"%d == %d",monthstr.intValue,month.intValue);
            if (monthstr.intValue == month.intValue) {
                [self.TimerArray2 removeAllObjects];

                for (int i=1; i<=daystr.integerValue; i++) {
                    
                    if (i<=9) {
                        [self.TimerArray2 addObject:[NSString stringWithFormat:@"0%d",i]];
                        
                    }else{
                        [self.TimerArray2 addObject:[NSString stringWithFormat:@"%d",i]];
                    }

                    
                }

            }else{
                [self.TimerArray2 removeAllObjects];

                    for (int i=1; i<=[self howManyDaysInThisYear:year.integerValue withMonth:month.integerValue]; i++) {
                        
                        if (i<=9) {
                            [self.TimerArray2 addObject:[NSString stringWithFormat:@"0%d",i]];
                            
                        }else{
                            [self.TimerArray2 addObject:[NSString stringWithFormat:@"%d",i]];
                        }
                    }

            }
            [self.TimerPicker reloadComponent:2];
            [self.TimerPicker selectRow:self.TimerArray2.count-1 inComponent:2 animated:YES];
            day = [self.TimerArray2 objectAtIndex:[self.TimerArray2 count]-1];


        }else if (component ==2){
        
            day = self.TimerArray2[row];

        }
        
        self.DateLabel.text = [NSString stringWithFormat:@"%@年-%@月-%@日",year,month,day];

    }
    
    
    
    
    
}




- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:UITextAlignmentCenter];
        [pickerLabel setTextColor:[UIColor whiteColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}


//判断月份日期
- (NSInteger)howManyDaysInThisYear:(NSInteger)year withMonth:(NSInteger)month{
    if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
        return 31 ;
    
    if((month == 4) || (month == 6) || (month == 9) || (month == 11))
        return 30;
    
    if((year % 4 == 1) || (year % 4 == 2) || (year % 4 == 3))
    {
        return 28;
    }
    
    if(year % 400 == 0)
        return 29;
    
    if(year % 100 == 0)
        return 28;
    
    return 29;
}

//显示月份还是月份加日期
- (IBAction)DateSelection:(UIButton *)sender {
    
    
    if (isdate ==YES) {
        [sender setTitle:@"按月选择" forState:UIControlStateNormal];
        isdate =NO;
        [self.TimerPicker reloadAllComponents];

        [self.TimerPicker selectRow:self.TimerArray2.count-1 inComponent:2 animated:NO];
        day = [self.TimerArray2 objectAtIndex:[self.TimerArray2 count]-1];

        self.DateLabel.text = [NSString stringWithFormat:@"%@年-%@月-%@日",year,month,day];

    }else{
        isdate =YES;
        [self.TimerPicker reloadAllComponents];
        [sender setTitle:@"按日选择" forState:UIControlStateNormal];
        self.DateLabel.text = [NSString stringWithFormat:@"%@年-%@月",year,month];


    }
    
}
//取消
- (IBAction)cancel:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        self.frame =CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 333);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];

    }];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"cancel" object:nil userInfo:nil];

}

//确认
- (IBAction)confirm:(id)sender {
    
    NSDictionary *dict = @{@"date":self.DateLabel.text};

    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"confirm" object:nil userInfo:dict];

    [UIView animateWithDuration:0.5 animations:^{
        self.frame =CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 333);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        
    }];

    
}

-(NSMutableArray *)TimerArray
{
    if (_TimerArray == nil) {
        _TimerArray = [[NSMutableArray alloc]init];
    }
    return _TimerArray;
}


-(NSMutableArray *)TimerArray1
{
    if (_TimerArray1 == nil) {
        _TimerArray1 = [[NSMutableArray alloc]init];
    }
    return _TimerArray1;
}

-(NSMutableArray *)TimerArray2
{
    if (_TimerArray2 == nil) {
        _TimerArray2 = [[NSMutableArray alloc]init];
    }
    return _TimerArray2;
}


@end
